package com.example.tt;

import java.util.Observable;
import java.util.concurrent.*;

import static java.lang.Thread.sleep;

/**
 * @Author: wws
 * describe:
 */
public class Boo extends Observable implements Runnable {


    private LinkedBlockingQueue<String> queue;

    public Boo(LinkedBlockingQueue<String> queue) {
        super();
        this.queue = queue;
    }

    @Override
    public void run() {
        for (; ; ) {
            try {
                String take = queue.take();
                System.out.println("BOO收到了数据");
                System.out.println(take);
                setChanged();
                notifyObservers(take);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
        Boo b = new Boo(queue);
        new Thread(b).start();
        ObServer o = new ObServer();
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        Future<String> future = threadPool.submit(o);
        b.addObserver(o);
        String a = "1";
        for (int i = 0; i < 5; i++) {
            queue.put(a);
            a += 1;
            sleep(1000);
        }
        System.out.println(future.get());
    }
}
